<?php

/**
 * This is the model class for table "otherincomes".
 *
 * The followings are the available columns in table 'otherincomes':
 * @property integer $id
 * @property string $income
 * @property double $sum
 * @property string $paydate
 *
 * The followings are the available model relations:
 * @property Otherincome $oincome
 */
class Otherincomes extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Otherincomes the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'otherincomes';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('income, sum, paydate', 'required'),
			array('sum', 'numerical'),
			array('income', 'length', 'max'=>10),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, income, sum, paydate', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'oincome' => array(self::BELONGS_TO, 'Otherincome', 'income'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'income' => 'Income',
			'sum' => 'Sum',
			'paydate' => 'Paydate',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria(array("with"=>"oincome"));

		$criteria->compare('id',$this->id);
		$criteria->compare('income',$this->income,true);
		$criteria->compare('sum',$this->sum);
		$criteria->compare('paydate',$this->paydate,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
	
	public function getTotals($provider)
	{
		$totals = array();
		$sumincomes = 0;
		$sumincomescnt = 0;
		$data = $provider->getData();
		foreach($data as $p)
		{	
			$sumincomes += $p->sum;
			$sumincomescnt++;
		}
		$totals['sumincomes']=$sumincomes;
		$totals['sumincomescnt']=$sumincomescnt;

		return $totals;
	}
}